$(function () {
    var checklist_datagrid = $("#checklist_datagrid");
    var checklist_dialog = $("#checklist_dialog");
    var checklist_form = $("#checklist_form");
    var checkpay_dialog=$("#checkpay_dialog");
    var checkpay_form=$("#checkpay_form");

    var cmdObj = {
        //工具栏的刷新
        reload: function () {
            checklist_datagrid.datagrid("reload");
        },

        //查看案件详情
        edit: function () {
            //判断是否选中数据
            var row = checklist_datagrid.datagrid("getSelected");
            /*如果没选中数据*/
            if (!row) {
                $.messager.alert('温馨提示', "请选择要查看的数据");
                return;
            }
            //添加与编辑时将数据清空
            checklist_form.form("clear");

            if (row.client) {
                row["client.name"] = row.client.name;
            }
            //数据回显: 同名原则
            checklist_form.form("load", row);
            //1.弹框
            checklist_dialog.dialog("open");
            //设置标题
            checklist_dialog.dialog("setTitle", "案件详情");
        },




        //查看赔偿明细
        look: function () {
            var row = checklist_datagrid.datagrid("getSelected");
            /*如果没选中数据*/
            if (!row) {
                $.messager.alert('温馨提示', "请选择要查看的数据");
                return;
            }
            /*把值设置进去*/
            $("#number").textbox("setValue", row.guaranteeBill.number);

            //添加与编辑时将数据清空

            $.get('/compensationInfo/selectByAid', {aid: row.id}, function (data) {

                //判断是否选中数据
                checkpay_form.form('load', data);//加载记录来填充表单
                //1.弹框
                checkpay_dialog.dialog("open");
                //设置标题
                checkpay_dialog.dialog("setTitle", "查看赔偿明细");
            })

        },

        //审核
        changeState: function () {
            //判断是否选中数据
            var row = checklist_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选择要编辑的数据");
                return;
            }
            $.messager.confirm('确认', '您确认审核吗？', function (r) {
                if (r) {
                    $.get("/accident/changeStatus2", {id: row.id,status:row.status}, function (data) {
                        if (data.success) {
                            //刷新列表数据
                            checklist_datagrid.datagrid("reload");
                        } else {
                            $.messager.alert('温馨提示', data.msg);
                        }
                    })
                }
            });
        },

        /*拒审*/
        refuse: function () {
            //判断是否选中数据
            var row = checklist_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选择要编辑的数据");
                return;
            }
            $.messager.confirm('确认', '您确认拒绝审核吗？', function (r) {
                if (r) {
                    $.get("/accident/changeStatus5", {id: row.id,status:row.status}, function (data) {
                        if (data.success) {
                            //刷新列表数据
                            checklist_datagrid.datagrid("reload");
                        } else {
                            $.messager.alert('温馨提示', data.msg);
                        }
                    })
                }
            });
        },

        //关闭
        cancel: function () {
            checklist_dialog.dialog("close");
        },


        //关闭
        cancel1: function () {
            checkpay_dialog.dialog("close");
        }
    }

    //调用
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });

    checklist_datagrid.datagrid({
        url: "/accident/list2",
        fit: true,
        fitColumns: true,
        striped: true,
        singleSelect: true,
        pagination: true,
        rownumbers: true,
        columns: [[
            {field: 'id', checkbox: true},
            {
                field: 'status', title: '当前状态', width: 80, formatter: function (value, row, index) {
                    if(value==0){
                        return "<font color='#a52a2a'>待验证</font>";
                    }
                    else if (value == 1) {
                        return "<font color='#00008b'>验证完,待填理赔</font>";
                    } else if (value == 2) {
                        return "<font color='#00ced1'>废弃</font>";
                    } else if (value == 3) {
                        return "<font color='#6495ed'>待审核</font>";
                    } else if (value == 4) {
                        return "<font color='#daa520'>被拒审核</font>";
                    } else if (value == 5) {
                        return "<font color='#ff6347'>审核完,待付款给保人</font>";
                    } else if (value == 6) {
                        return "<font color='red'>已赔付</font>";
                    }
                    return false;
                }
            },
            {
                field: "client", title: "报案人姓名", width: 100,
                formatter: function (value, row, index) {
                    if (value) {
                        return value.name;
                    } else {
                        return "";
                    }
                }
            },
            {
                field: "guaranteeBill", title: "保险单号", width: 100, formatter: function (value, row, index) {
                    if(value==null){
                        return;
                    }
                    return value.number;
                }
            },
            {
                field: "tel", title: "联系方式", width: 100, formatter: function (value, row, index) {
                    if (row.client) {
                        return row.client.tel;
                    } else {
                        return "";
                    }
                }
            },
            {field: 'oTime', title: '报案时间', width: 80},
            {field: 'duty', title: '事故责任', width: 80},
            {
                field: "inputUser", title: "录入人", width: 100, formatter: function (value, row, index) {
                    if (value) {
                        return value.username;
                    } else {
                        return "";
                    }
                }
            },
            {field: 'remark', title: '备注', width: 80}
        ]],
        toolbar: '#tb'
    });
    checklist_dialog.dialog({
        width: 380,
        height: 380,
        buttons: "#bb",
        closed: true
    })
    checkpay_dialog.dialog({
        width: 380,
        height: 380,
        buttons: "#cc",
        closed: true
    })
});



